package com.zjf.carpark.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;


/**
 * Created with IntelliJ IDEA.
 * User: 张建飞
 * Date: 2020/01/30
 * Time: 15:23
 * To change this template use File | Settings | File Templates.
 * Description: 短信发送工具类，用于短信发送
 */
public class SendSmsUtil {

    /**
     * 发送一条短信
     *
     * @param phoneNumbers 发送到的电话号码
     * @param validateCode 发送出去的验证码
     * @return 发送后的响应数据
     */
    public static String sendSms(String phoneNumbers, int validateCode) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4FgZnnPdEFnUW34TpCZq",
                "IYf2YsevwR4JcgOJueDtfZNU5I6jIA");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        //签名
        request.putQueryParameter("SignName", "小智停车场");
        //短信模板 SMS_186599013
        request.putQueryParameter("TemplateCode", "SMS_180947211");
        String code = "{\"code\":\"" + validateCode + "\"}";
        request.putQueryParameter("TemplateParam", code);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
